var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    // 上传瓦工合同
    contractList: [],
    // 是否大板工地经验
    switch1Checked1: 0,
    // 店长上传工地图:
    workList: [],
    // 是否是否经过培训
    switch1Checked2: 0,
    // 手持培训图
    trainList: [],
    issuccessMask: false
  },
  // 上传附件
  upload(arr) {
    return new Promise((reslove, reject) => {
      if (arr.length == 0) {
        reslove([])
        return
      }
      wx.showLoading({
        title: '上传中...', //提示的内容
        mask: true //显示透明蒙层 防止触摸穿透
      })
      let finishFilesList = []
      arr.forEach((item, index) => {
        wx.uploadFile({
          url: app.globalData.Interfaceurl + 'saveFileUpload',
          filePath: item,
          name: 'file',
          formData: {
            userId: app.globalData.userId,

          },
          header: {
            "content-type": "multipart/form-data"
          },
          success: function (res) {
            finishFilesList.push(JSON.parse(res.data).fileUrl)
            if (index >= arr.length - 1) {
              setTimeout(()=>{
                wx.hideLoading();
                reslove(finishFilesList)
               },500)
            }
          },
          fail: err => {
            return err
          }
        })
      })
    })


  },
  saveBricklay() {
    // console.log(this.data.bricklayObj.trainProof.join(","),)
    // return
    wx.showLoading({
      title: '加载中',
      
      mask: true //显示透明蒙层 防止触摸穿透
    })
    let t = this
    wx.request({
      url: app.globalData.Interfaceurl + 'saveBricklay',
      method: "post",
      header: {
        "content-type": "application/x-www-form-urlencoded;charset=UTF-8"
      },
      data: {
        userId: app.globalData.userId,
        token: t.data.token,
        bricklayId: t.data.bricklayObj.bricklayId,
        name: t.data.bricklayObj.name,
        mobile: t.data.bricklayObj.mobile,
        sex: t.data.bricklayObj.sex,
        workType: t.data.bricklayObj.workTypeObj.workIndex,
        province: t.data.bricklayObj.province,
        city: t.data.bricklayObj.city,
        area:t.data.bricklayObj.area,
        beginWorkDate: t.data.bricklayObj.beginWorkDate,
        contractProof: t.data.bricklayObj.contractProof.join(","),
        workFlag: t.data.bricklayObj.workFlag,
        workProof: t.data.bricklayObj.workProof.join(","),
        trainFlag: t.data.bricklayObj.trainFlag,
        trainProof: t.data.bricklayObj.trainProof.join(","),
      },
      success: res => {
        wx.hideLoading()
        console.log(res)
        if (res.data.result == 0) {
          this.setData({
            issuccessMask: true
          })
        } else {
          wx.showToast({
            title: res.data.message,
            duration: 2500, //持续的时间
            icon: 'none', //图标
          })
          t.setData({
            token:app.token
          })
        }
      },
      fail: err => {
        wx.showToast({
          title: '请求失败',
          duration: 2500, //持续的时间
          icon: 'none', //图标
        })
      }
    })
  },


  add() {

    if (this.data.contractList.length == 0) {
      wx.showToast({
        title: '请上传瓦工合同',
        duration: 2500, //持续的时间
        icon: 'none', //图标
      })
      return
    }

    if (this.data.switch1Checked1 == 1 && this.data.workList.length == 0) {
      wx.showToast({
        title: '请上传工地图片',
        duration: 2500, //持续的时间
        icon: 'none', //图标
      })
      return
    }

    if (this.data.switch1Checked2 == 1 && this.data.trainList.length == 0) {
      wx.showToast({
        title: '请上传瓦工培训图片',
        duration: 2500, //持续的时间
        icon: 'none', //图标
      })
      return
    }


    let bricklayObj = this.data.bricklayObj
    bricklayObj.workFlag = this.data.switch1Checked1
    bricklayObj.trainFlag = this.data.switch1Checked2


    if (bricklayObj.bricklayId == '') {
      this.upload(this.data.contractList)
        .then(res => {
          bricklayObj.contractProof = res
          this.upload(this.data.workList)
            .then(res => {
              bricklayObj.workProof = res

              this.upload(this.data.trainList)
                .then(res => {
                  bricklayObj.trainProof = res
                  this.setData({
                    bricklayObj: bricklayObj
                  })
                  console.log(this.data.bricklayObj)
                  this.saveBricklay()
                })
            })
        })
    } else {
      let uncontractList = this.data.contractList.filter(item => item.indexOf(this.data.Imageurl) != -1)
      uncontractList = uncontractList.map(item => item.replace(this.data.Imageurl, ""))
      let nocontractList = this.data.contractList.filter(item => item.indexOf(this.data.Imageurl) == -1)


      let unworkList = this.data.workList.filter(item => item.indexOf(this.data.Imageurl) != -1)
      unworkList = unworkList.map(item => item.replace(this.data.Imageurl, ""))
      let noworkList = this.data.workList.filter(item => item.indexOf(this.data.Imageurl) == -1)
      console.log(unworkList, noworkList, this.data.workList)


      let untrainList = this.data.trainList.filter(item => item.indexOf(this.data.Imageurl) != -1)
      untrainList = untrainList.map(item => item.replace(this.data.Imageurl, ""))

      let notrainList = this.data.trainList.filter(item => item.indexOf(this.data.Imageurl) == -1)


      this.upload(nocontractList)
        .then(res => {
          bricklayObj.contractProof = [...res, ...uncontractList]
          this.upload(noworkList)
            .then(res => {

              bricklayObj.workProof = [...res, ...unworkList]
              this.upload(notrainList)
                .then(res => {
                  bricklayObj.trainProof = [...res, ...untrainList]
                  this.setData({
                    bricklayObj: bricklayObj
                  })
                  this.saveBricklay()
                })
            })
        })




    }






  },




  HidedispatcCar() {
    this.setData({
      issuccessMask: false
    })
    let pages = getCurrentPages();
    let prevPage = pages[pages.length - 3]; //prevPage 是获取上一个页面的js里面的pages的所有信息。 -2 是上一个页面，-3是上上个页面以此类推。\\
    prevPage.getBricklayList()


    wx.navigateBack({
      delta: 2,
    })
  },




  Unplonecover(e) {
    this.setData({
      contractList: e.detail
    })
  },
  Unplonewor(e) {
    this.setData({
      workList: e.detail
    })
  },
  Unplonetrain(e) {
    this.setData({
      trainList: e.detail
    })
  },
  // 是否有工地经验
  switch1Change1() {
    this.setData({
      switch1Checked1: this.data.switch1Checked1 == 0 ? 1 : 0
    })
    if(this.data.switch1Checked1==0){
      this.setData({
        workList: []
      })
    }
  },
  // 是否经过培训
  switch1Change2() {
    // if(this.data.bricklayObj.bricklayId!=''&&this.data.bricklayObj.trainFlag==1){
    //   this.setData({
    //     switch1Checked2:  1
    //   })
    //   return
    //   }
    this.setData({
      switch1Checked2: this.data.switch1Checked2 == 0 ? 1 : 0
    })
    if(this.data.switch1Checked2==0){
      this.setData({
        trainList: []
      })
    }
  },


  fakeCallback() {},

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      token: app.token(),
      Imageurl: app.globalData.Imageurl
    })
    if (options.todo) {
      this.setData({
        todo: options.todo
      })
    }
    if (options.bricklayObj) {
      this.setData({
        bricklayObj: JSON.parse(options.bricklayObj)
      })

      if (this.data.bricklayObj.bricklayId != '') {
        let bricklayObj = this.data.bricklayObj
        let contractList = bricklayObj.contractProof != '' ? bricklayObj.contractProof.split(",").map(item => this.data.Imageurl + item) : []
        let switch1Checked1 = bricklayObj.workFlag
        let workList = bricklayObj.workProof != '' ? bricklayObj.workProof.split(",").map(item => this.data.Imageurl + item) : []
        let switch1Checked2 = bricklayObj.trainFlag
        let trainList = bricklayObj.trainProof != '' ? bricklayObj.trainProof.split(",").map(item => this.data.Imageurl + item) : []
        this.setData({
          contractList: contractList,
          switch1Checked1: switch1Checked1,
          workList: workList,
          switch1Checked2: switch1Checked2,
          trainList: trainList
        })
      }

    }

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})